package com.xrz.sxm.aj.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.xrz.sxm.aj.activity.R;

public class BaseDialog {

	private static Dialog dialog;

	private static void createDialog(Context ctx) {
		if (dialog == null) {
			dialog = new Dialog(ctx, R.style.dialogdialog);
			dialog.setCanceledOnTouchOutside(false);
			Window window = dialog.getWindow();
			window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
			window.setWindowAnimations(R.style.dialoganimationstyle); // 添加动画
		}
	}

	public static void dismiss() {
		dialog.dismiss();
		dialog = null;
	}

	/**
	 * 弹出自定义对话框
	 *
	 * @param ctx
	 *            上下文
	 * @param title
	 *            对话框的标题
	 * @param content
	 *            对话框的正文
	 * @param bSignBtn
	 *            是否只有一个button（true:只有一个button）
	 * @param leftBtnText
	 *            左边按钮的文字
	 * @param rightBtnText
	 *            右边按钮的文字
	 * @param btn1ClickListener
	 *            左边button的点击事件（可以为null）
	 * @param btn2ClickListener
	 *            右边button的点击事件（可以为null）
	 */
	public static void showDialog(Context ctx, String title, String content,
								  boolean bSignBtn, String leftBtnText, String rightBtnText,
								  final onDialogBtnClickListener btn1ClickListener,
								  final onDialogBtnClickListener btn2ClickListener) {
		createDialog(ctx);
		View view = LayoutInflater.from(ctx).inflate(R.layout.layout_dialog,
				null);
		TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
		TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
		Button btn1 = (Button) view.findViewById(R.id.btn_1);
		Button btn2 = (Button) view.findViewById(R.id.btn_2);
		btn1.setText(leftBtnText);
		btn2.setText(rightBtnText);
		android.widget.LinearLayout.LayoutParams lp1 = (android.widget.LinearLayout.LayoutParams) btn1
				.getLayoutParams();

		if (bSignBtn) {
			btn2.setVisibility(View.GONE);
		} else {
			btn2.setVisibility(View.VISIBLE);
			lp1.rightMargin = 1;
		}
		tv_title.setText(title);
		tv_content.setText(content);
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				dialog = null;
				if (btn1ClickListener != null) {
					btn1ClickListener.onClick();
				}
			}
		});
		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				dialog = null;
				if (btn2ClickListener != null) {
					btn2ClickListener.onClick();
				}
			}
		});

		int width = ScreenUtil.getInstant().getDisWidth(ctx);
		LayoutParams layoutParams = new LayoutParams(width - 60,
				LayoutParams.WRAP_CONTENT);
		dialog.setContentView(view, layoutParams);
		dialog.show();
	}

	public interface onDialogBtnClickListener {
		void onClick();
	}
}
